package com.mach.platform.sprider.dev;

import lombok.extern.slf4j.Slf4j;
import org.jinterop.dcom.common.JIException;
import org.openscada.opc.lib.common.ConnectionInformation;
import org.openscada.opc.lib.da.*;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;


@Slf4j
public class DevServiceZZ {


    public static final String PLKEY = "_PL";
    public static final String VLKEY = "_VL";
    private static DevServiceZZ me = new DevServiceZZ();

    public static DevServiceZZ get(){return DevServiceZZ.me;}

    private Map<String, DataHelper> devsMap = new HashMap<String, DataHelper>();

    public DevServiceZZ(){


        //冲压车间电表模块:抓取电压，电流，功率

//        devsMap.put("1#1" + VLKEY, new CongYaDBDev(  "192.168.1.200",8001,1,"1#1" + VLKEY));
//        devsMap.put("1#2" + VLKEY, new CongYaDBDev(  "192.168.1.200",8001,2,"1#2" + VLKEY));
//        devsMap.put("1#3" + VLKEY, new CongYaDBDev(  "192.168.1.200",8001,3,"1#3" + VLKEY));
//        devsMap.put("1#4" + VLKEY, new CongYaDBDev(  "192.168.1.200",8001,4,"1#4" + VLKEY));
//
//        devsMap.put("3#1" + VLKEY, new CongYaDBDev(  "192.168.1.200",8003,1,"3#1" + VLKEY));
//        devsMap.put("3#2" + VLKEY, new CongYaDBDev(  "192.168.1.200",8003,2,"3#2" + VLKEY));
//        devsMap.put("3#3" + VLKEY, new CongYaDBDev(  "192.168.1.200",8003,3,"3#3" + VLKEY));


    }




    /*
        冷冻水:供水温度
     */
    public Float getCoolWaterImportTemp(String devId) throws IOException {
        Float result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataTcp(4, 2);
        byte[] data = ByteUtil.subData(repData,9,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = ByteUtil.getFloat(data);

        return result;
    }

    /*
        冷冻水:回水温度
     */
    public Float getCoolWaterBackTemp(String devId) throws IOException {
        Float result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataTcp(40, 2);
        byte[] data = ByteUtil.subData(repData,9,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = ByteUtil.getFloat(data);

        return result;
    }
    /*
        冷冻水:累计流量
     */
    public Integer getCoolWaterSumFlow(String devId) throws IOException {
        Integer result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataTcp(8, 2);
        byte[] data = ByteUtil.subData(repData,9,4);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        result = ByteUtil.getInt(data);

        return result;
    }

    /*
      冷冻水:累计冷量
   */
    public Integer getCoolWaterSumCool(String devId) throws IOException {
        Integer result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataTcp(12, 2);
        byte[] data = ByteUtil.subData(repData,9,4);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        result = ByteUtil.getInt(data);

        return result;
    }

    /*
      压缩空气：实时流量
     */
    public Float getCpAirRealTimeFlow(String devId) throws IOException {
        Float result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

//        byte[] repData = dh.getDataRtu(6,2);
//        byte[] data = ByteUtil.subData(repData,4,4);
//        data = ByteUtil.reverse2Byte(data);
//        result = ByteUtil.getFloat(data);

        byte[] repData = dh.getDataRtu(6,2);
        byte[] data = ByteUtil.subData(repData,3,4);
        ByteUtil.reveIn2Byte(data, 0);
        ByteUtil.reveIn2Byte(data, 2);
        result = ByteUtil.getFloat(data);

        return result;
    }

    /*
        压缩空气：累积流量
     */
    public Integer getCpAirSumFlow(String devId) throws IOException {
        Integer result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataRtu(12,2);
        byte[] data = ByteUtil.subData(repData,3,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = new Integer(ByteUtil.getInt(data));

        return result;
    }

    public Integer getTransducerStatus(String devName){
        GerenalStatusDev dh = (GerenalStatusDev)devsMap.get(devName);
        if(dh == null)return null;
        Integer rt = dh.getStatus();

        return rt;
    }

    public Float getTransducerPl(String devName){
        TransducerPlDev dh = (TransducerPlDev)devsMap.get(devName + PLKEY);

        if(dh == null)return null;
        Float rt = dh.getPl();

        return rt;
    }

    /*
    取变频器:电压
 */
    public Float getTransducerVolt(String devId) {
        Float result = null;

        TransducerDBDev db = (TransducerDBDev)devsMap.get(devId + VLKEY);
        if(db == null)return null;
        result = db.getVolt();


        return result;
    }

    /*
     取变频器:电流
  */
    public Float getTransducerElect(String devId) {
        Float result = null;

        TransducerDBDev db = (TransducerDBDev)devsMap.get(devId + VLKEY);
        if(db == null)return null;

        result = db.getElect();


        return result;
    }

    /*
     取变频器:功率
  */
    public Float getTransducerPower(String devId) {
        Float result = null;

        TransducerDBDev db = (TransducerDBDev)devsMap.get(devId + VLKEY);
        if(db == null)return null;
        result = db.getPower();


        return result;
    }

    /*
    取露点仪温度
 */
    public Float getLuDianTemp(String devId) {
        Float result = null;

        LuDianKunHanDev db = (LuDianKunHanDev)devsMap.get(devId);
        if(db == null)return null;
        result = db.getTemp();


        return result;
    }

    public void startDevDataReadService() throws IOException {

        Collection<DataHelper> devs = devsMap.values();

        for(DataHelper dh : devs){
            Thread th = new Thread(dh);
            th.start();
        }

    }



    public static void main(String[] args) throws IOException {
        //get().startDevDataReadService();
        get().startZZElectOpc();
    }

    public void startZZElectOpc(){

        // create connection information
        final ConnectionInformation ci = new ConnectionInformation();
        ci.setHost("192.168.1.10");
        ci.setDomain("");
        ci.setUser("administrator");
        ci.setPassword("123-abc");
        ci.setClsid("75d00bbb-dda5-11d1-b944-9e614d000000");
        ci.setProgId("OPCServer.WinCC.1");

//        ci.setHost("192.168.32.129");
//        ci.setDomain("");
//        ci.setUser("mvmv");
//        ci.setPassword("windows");
//        ci.setProgId("OPCServer.WinCC");
//        ci.setClsid("75d00bbb-dda5-11d1-b944-9e614d000000");

        // ci.setClsid("680DFBF7-C92D-484D-84BE-06DC3DECCD68"); // if ProgId is not working, try it using the Clsid instead
        final String itemId = "01_E100A_Meter_Yc.Ia";
        // create a new server
        final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());

        try {
            // connect to server
            server.connect();
            // add sync access, poll every 500 ms
            final AccessBase access = new SyncAccess(server, 500);
            access.addItem(itemId, new DataCallback() {
                @Override
                public void changed(Item item, ItemState state) {
                    System.out.println(state);
                }
            });
            // start reading
            access.bind();
            // wait a little bit
            Thread.sleep(1 * 1000);
            // stop reading
            access.unbind();
        } catch (final JIException e) {
            System.out.println(String.format("%08X: %s", e.getErrorCode(), server.getErrorMessage(e.getErrorCode())));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}


////压缩空气 名称
//$scope.compressedAirNames= [{
//        "name": '1.0 总管流量'}, {
//        "name": '0.8 (12万) 总管流量'}, {
//        "name": '0.8 (24万) 总管流量'}, {
//        "name": '三线总管流量'}, {
//        "name": 'PR2流量'}, {
//        "name": 'AF2流量'}, {
//        "name": 'WE2流量'}, {
//        "name": 'PA2流量'}, {
//        "name": 'PO2流量'}, {
//        "name": '轮胎线流量'}];
//
////冷冻水--首页--动态实时数据名称
//        $scope.coldWaterNames= [{
//        "name": "AF2车间"}, {
//        "name": "PR2车间"}, {
//        "name": "WE2车间"}, {
//        "name": "PA2车间"}, {
//        "name": "PO2车间"}, {
//        "name": "PA2/PO2、24h车间"}];
